package com.anli.service;

import com.alibaba.fastjson.JSON;
import com.anli.dao.StockDao;
import com.anli.redisSuo.MyRedisLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/8  14:27
 * 描述:
 */
@Component
public class LockStockService {

    @Autowired
    private RedisTemplate<String,Object> myRedisTemplate;

    @Autowired
    private StockDao stockDao;

    /**
     * 作者: LCG
     * 日期: 2021/3/8  14:51
     * 描述: 锁库存
     * @Param [goodsId, clientId, expire, lockStock]
     * @Return void
     */
    public void stockGoods(String goodsId, String clientId, Long expire,int lockStock){

            boolean lock = MyRedisLock.lock(goodsId, clientId, expire, myRedisTemplate);
            if(lock){
               try{
                       lockStock(goodsId,lockStock,clientId);
                   }finally {
                       MyRedisLock.unLock(goodsId,clientId,myRedisTemplate);
               }
            }else{
               /* while (true){
                    boolean lock2 = MyRedisLock.lock(goodsId, clientId, expire, redisTemplate2);
                    if(lock2){
                        try{
                            lockStock(goodsId,lockStock,clientId);
                        }finally {
                            MyRedisLock.unLock(goodsId,clientId,redisTemplate2);
                        }
                        break;
                    }
                }*/

                Map<String,String> map=new HashMap<>();
                map.put("goodsId",goodsId);
                map.put("clientId",clientId);
                map.put("expire",expire.toString());
                map.put("lockStock",lockStock+"");
                String jsonString = JSON.toJSONString(map);
                myRedisTemplate.convertAndSend("LOCK",jsonString);
            }

    }


    public void lockStock(String goodsId,int lockStock,String clientId){
            int stock = stockDao.getStock(goodsId);
            //1 查看库存足不足
            if(lockStock < stock){
                //2 如果库存足量减少库存  更新库存
                Map<String,Object> map=new HashMap<>();
                map.put("goodsId",goodsId);
                map.put("stock",stock-lockStock);
                System.out.println("========进入锁库存=====库存减一>1");
                stockDao.updateStock(map);
            }else{
                //3 如果库存不足则挂起当前订单 等待补货
            }
    }


    @Autowired
    private RedissonClient redissonClient;


    public void stockGoods2(String goodsId, String clientId, Long expire,int lockStock){
        RLock lock = redissonClient.getLock(goodsId);

        lock.lock();
        lockStock(goodsId,lockStock,clientId);
        lock.unlock();
    }

}
